﻿/* 
 * Godtear.com
 * QQ:bzure@189.cn
 *Author:Nathan Liu
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Bzure.Common.ObjectFactory;


namespace Bzure.Common.Configuration.AppConfiguration
{
    public abstract class ConfigurationBase : IConfiguration
    {
        protected Dictionary<string, IEnumerable<ConfigurationSection>> sections = new Dictionary<string, IEnumerable<ConfigurationSection>>();
        protected XmlNode ConfigNode;
        public ConfigurationBase()
        {
        }
        #region IAppConfiguration
        public T[] GetConfigurationSections<T>() where T : ConfigurationSection, new()
        {
            List<T> list = new List<T>();
            foreach (ConfigurationSection section in LoadSectionbyType<T>())
                list.Add(section as T);
            return list.ToArray();
        }
        public T GetFirstConfigurationSection<T>() where T : ConfigurationSection, new()
        {
            List<T> list = new List<T>();
            foreach (ConfigurationSection section in LoadSectionbyType<T>())
                list.Add(section as T);
            return list.Count > 0 ? list[0] : null;
        }
        public T GetConfigurationSection<T>(string name) where T : ConfigurationSection, new()
        {
            foreach (ConfigurationSection section in LoadSectionbyType<T>())
                if (section.Name.Equals(name))
                    return section as T;
            return default(T);
        }
        IEnumerable<ConfigurationSection> LoadSectionbyType<T>() where T : ConfigurationSection, new()
        {
            T obj = ReflectionFactory.Create<T>().Build();
            if (obj != null)
            {
                IEnumerable<ConfigurationSection> subSections = null;
                if (!sections.TryGetValue(obj.BlockName, out subSections))
                {
                    List<ConfigurationSection> sublist = new List<ConfigurationSection>();
                    foreach (XmlNode node in GetSubNode(obj.BlockName))
                    {
                        T tobj = ReflectionFactory.Create<T>().Build();
                        tobj.Load(node);
                        sublist.Add(tobj);
                    }
                    subSections = sublist;
                }
                return subSections;
            }
            return null;
        }
        IEnumerable<XmlNode> GetSubNode(string nodeName)
        {
            foreach (XmlNode node in ConfigNode.ChildNodes)
                if (node.Name.Equals(nodeName))
                    yield return node;
        }
        public virtual void Load(XmlNode node)
        {
            ConfigNode = node;
        }
        protected string GetProperty(string property)
        {
            if (this.ConfigNode.Attributes[property] == null)
                return null;
            return this.ConfigNode.Attributes[property].Value;
        }
        #endregion IAppConfiguration
    }
}
